package com.duolebo.uteped_sdk.msg;

import androidx.annotation.NonNull;

import com.duolebo.uteped_sdk.UTEPedEventHandler;
import com.duolebo.uteped_sdk.utils.Const;
import com.yc.nadalsdk.bean.ActivityRemind;
import com.yc.nadalsdk.bean.Response;
import com.yc.nadalsdk.ble.open.UteBleConnection;

import java.util.HashMap;
import java.util.Map;

import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;

/**
 * 久坐提醒接口
 */
public class SedentaryMessage extends BaseMessage{

    private final static String PATH = "sedentary";

    private final static String M_setActivityRemind = "setActivityRemind";
    private final static String M_getActivityRemind = "getActivityRemind";

    /**
     * BaseCallback
     *
     * @param methodChannel methodChannel
     * @param eventHandler eventHandler
     */
    public SedentaryMessage(MethodChannel methodChannel, UTEPedEventHandler eventHandler) {
        super(methodChannel, eventHandler);
    }

    @Override
    public String getPath() {
        return PATH;
    }

    @Override
    public boolean methodCallConsumed(@NonNull MethodCall call, @NonNull MethodChannel.Result result) {
        if (!call.hasArgument(Const.KEY_METHOD))
            return false;

        if (super.methodCallConsumed(call, result))
            return true;

        UteBleConnection uteBleConnection = uteBleClient.getUteBleConnection();
        boolean consumed = true;
        boolean success = false;
        String method = call.argument(Const.KEY_METHOD);
        assert method != null;
        switch (method) {
            case M_setActivityRemind:
                if (uteBleClient.isConnected()) {
                    Response<?> rsp = uteBleConnection.setActivityRemind(getActivityRemind(call));
                    success = rsp.getErrorCode() == Const.CODE_SUCCESS;
                }
                result.success(success);
                break;

            case M_getActivityRemind:
                Response<ActivityRemind> arRsp = uteBleConnection.getActivityRemind();
                Map<String, Object> map = new HashMap<>();
                map.put(Const.KEY_SUCCESS, arRsp.isSuccess());
                if (arRsp.isSuccess()) {
                    map.putAll(objectToMap(arRsp.getData(), true, false));
                }
                result.success(map);
                break;

            default:
                consumed = false;
        }

        return consumed;
    }

    private ActivityRemind getActivityRemind(MethodCall call) {
        ActivityRemind remind;
        Response<ActivityRemind> arRsp = uteBleClient.getUteBleConnection().getActivityRemind();
        if (arRsp.getErrorCode() == Const.CODE_SUCCESS) {
            remind = arRsp.getData();
        }
        else {
            remind = new ActivityRemind();
        }

        if (call.hasArgument(Const.KEY_ENABLE)) {
            boolean enable = Boolean.TRUE.equals(call.argument(Const.KEY_ENABLE));
            remind.setEnable(enable);
        }

        if (call.hasArgument(Const.KEY_fromTimeHour)) {
            int fromTimeHour = call.argument(Const.KEY_fromTimeHour);
            remind.setFromTimeHour(fromTimeHour);
        }

        if (call.hasArgument(Const.KEY_fromTimeMinute)) {
            int fromTimeMinute = call.argument(Const.KEY_fromTimeMinute);
            remind.setFromTimeMinute(fromTimeMinute);
        }

        if (call.hasArgument(Const.KEY_toTimeHour)) {
            int toTimeHour = call.argument(Const.KEY_toTimeHour);
            remind.setToTimeHour(toTimeHour);
        }

        if (call.hasArgument(Const.KEY_toTimeMinute)) {
            int toTimeMinute = call.argument(Const.KEY_toTimeMinute);
            remind.setToTimeMinute(toTimeMinute);
        }

        if (call.hasArgument(Const.KEY_remindInterval)) {
            int remindInterval = call.argument(Const.KEY_remindInterval);
            remind.setRemindInterval(remindInterval);
        }

        if (call.hasArgument(Const.KEY_cyclePeriod)) {
            int cyclePeriod = call.argument(Const.KEY_cyclePeriod);
            remind.setCyclePeriod(cyclePeriod);
        }

        return remind;
    }
}
